package com.toyota.toyotaownerspoc.takeAPicture;

import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import android.content.Context;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.hardware.Camera.CameraInfo;
import android.hardware.Camera.Size;
import android.os.Environment;
import android.util.Log;
import android.widget.Toast;

public class CameraUtility {

	
	/* find camera on device */
	public static int findFrontFacingCamera() {
		int cameraId = -1;
		// Search for the front facing camera
		int numberOfCameras = Camera.getNumberOfCameras();
		for (int i = 0; i < numberOfCameras; i++) {
			CameraInfo info = new CameraInfo();
			Camera.getCameraInfo(i, info);
			if (info.facing == CameraInfo.CAMERA_FACING_BACK) {
				Log.d("ClassScanViewBarcodeActivity , findFrontFacingCamera(): ",
						"Camera found");
				cameraId = i;
				break;
			}
		}
		return cameraId;
	}// end findFrontFacingCamera()

	/*
	 * created savePictureInSDcardPIcturesDirectory(byte [] data) for testing
	 */
	public static void savePictureInSDcardPicturesDirectory(byte[] data, String title) {
		Log.d("ScanVinFromBarcodeActivity ", "savePicture(byte [] data)");

		String photoFile = title +".jpg";

		File sdDir = Environment
				.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);

		String filename = sdDir + File.separator + photoFile;

		File pictureFile = new File(filename);

		try {
			FileOutputStream fos = new FileOutputStream(pictureFile);
			fos.write(data);
			fos.close();

			Log.d("savePicture() New Image saved:", photoFile);

		} catch (Exception error) {
			Log.d("File not saved: ", error.getMessage());
			
		
		}

	}
	

	
	
	
	public static Size getOptimalPreviewSize(List<Camera.Size> sizes, int w, int h) {
		final double ASPECT_TOLERANCE = 0.1;
		final double MAX_DOWNSIZE = 1.5;

		double targetRatio = (double) w / h;
		if (sizes == null)
			return null;

		Size optimalSize = null;
		double minDiff = Double.MAX_VALUE;

		int targetHeight = h;

		// Try to find an size match aspect ratio and size
		for (Camera.Size size : sizes) {
			double ratio = (double) size.width / size.height;
			double downsize = (double) size.width / w;
			if (downsize > MAX_DOWNSIZE) {
				// if the preview is a lot larger than our display surface
				// ignore it
				// reason - on some phones there is not enough heap
				// available to show the larger preview sizes
				continue;
			}
			if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE)
				continue;
			if (Math.abs(size.height - targetHeight) < minDiff) {
				optimalSize = size;
				minDiff = Math.abs(size.height - targetHeight);
			}
		}
		// Cannot find the one match the aspect ratio, ignore the
		// requirement
		// keep the max_downsize requirement
		if (optimalSize == null) {
			minDiff = Double.MAX_VALUE;
			for (Size size : sizes) {
				double downsize = (double) size.width / w;
				if (downsize > MAX_DOWNSIZE) {
					continue;
				}
				if (Math.abs(size.height - targetHeight) < minDiff) {
					optimalSize = size;
					minDiff = Math.abs(size.height - targetHeight);
				}
			}
		}
		// everything else failed, just take the closest match
		if (optimalSize == null) {
			minDiff = Double.MAX_VALUE;
			for (Size size : sizes) {
				if (Math.abs(size.height - targetHeight) < minDiff) {
					optimalSize = size;
					minDiff = Math.abs(size.height - targetHeight);
				}
			}
		}
		return optimalSize;
	}//end
	
	/*this method attempts to turn the devices torch light used for taking pictures */
	public static void turnOnFlashlight(Camera camera, Context context) {
		Log.d("ClassScanViewBarcodeActivity",
				"turnOnFlashlight(Camera camera )");

		Camera.Parameters Flash = null;
		if (camera != null) {
			
			if (context != null) {

					Flash = camera.getParameters();
					Flash.setFlashMode("torch");
					camera.setParameters(Flash);

			}// end if context not null

		}// end camera not null

	}// end turnOnFlashlight(Camera camera )
	
	//check if particular focus mode is supported
	public static boolean isFocusModeSupported(Camera camera, String parameter)
	{
		boolean flag = false;
		
	 if (camera!=null && parameter!=null)
	 {
		 List<String> list = camera.getParameters().getSupportedFocusModes();
		 
		 String value = null; 
		 for (int i =0 ; i<list.size(); i++)
		 {
			 value = list.get(i);
			 
			 if (value.equalsIgnoreCase(parameter))
			 {
				 Log.d("CameraUility, isFocusModeSupported() found parameter: ",value);
				 flag = true; 
				 break;
			 }
			 
		 }
	 }
	 return flag; 
	}//end isFocusModeSupported(Camera camera, String parameter)
	
	//check if the particular device supports flash 
	public static boolean isFlashSupported(Context context, Camera camera, String parameter)
	{
		boolean flag = false;
		
	 if (context!=null && camera!=null && parameter!=null)
	 {
		 List<String> list = camera.getParameters().getSupportedFlashModes();
		 
		 String value = null; 
		 for (int i =0 ; i<list.size(); i++)
		 {
			 value = list.get(i);
			 
			 if (value.equalsIgnoreCase(parameter))
			 {
				 flag = true; 
				 Log.d("CameraUility, isFlashSupported() found parameter: ",value);
				 break;
			 }
			 
		 }//end
		 boolean FEATURE_CAMERA_FLASH = context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
		 if (flag && FEATURE_CAMERA_FLASH)
		 {
			 flag = true;
		 }
	 }
	 
	
	 return flag; 
	}//end isFlashSupported(Context context, Camera camera, String parameter)
	
}
